<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Models\Link;
use App\Models\Category;
use App\Models\Question;
use App\Models\Tag;
class CommonController extends Controller
{
    /**
     * 处理前台分类数据
     */
    static public function getCatePid($pid = 0)
    {
        $data = DB::table('zy_category')->where('pid',$pid)->get();
        $arr = [];
        foreach ($data as $key => $value)
        {
            $value['sub'] = self::getCatePid($value['id']);
            $arr[] = $value;
        }
        return $arr;
    }
    /**
     * 处理前台搜索
     */
    static public function search(Request $request)
    {

        $search = $request -> input('search','');

        $tag = DB::table('zy_tag')->where('tag_name','like','%'.$search.'%')->first();

        $cid = $tag['cid'];

        return redirect('/home/list?id='.$cid);

    }

     /**
     * 处理前台个人中心数据
     */
    static public function getUser()
    {
        //获取登陆id
        $uid = session('login');
        //获取登录用户详细信息
        $user = DB::table('zy_userdetails')->where('uid',$uid)->first();
        return $user;

    }
    /**
     * 处理经验等级
     */
    static public function getLevel()
    {
        $user = DB::table('zy_userdetails')->join('zy_score','zy_userdetails.uid','=','zy_score.uid')->get();

         $level=[];

            foreach($user as $kk=>$vv)
            {
                if($vv['exp'] >= 0 && $vv['exp'] <= 10)
               {
                       $vv['level'] = "1";
               }elseif($vv['exp'] > 10 && $vv['exp'] <= 50)
               {
                       $vv['level'] = "2";
               }elseif($vv['exp'] > 50 && $vv['exp'] <= 100)
               {
                       $vv['level'] = "3";
               }elseif($vv['exp'] > 100 && $vv['exp'] <= 200)
               {
                       $vv['level'] = "4";
               }elseif($vv['exp'] > 200 && $vv['exp'] <= 500)
               {
                      $vv['level'] = "5";
               }elseif($vv['exp'] > 500 && $vv['exp'] <= 1000)
               {
                      $vv['level'] = "6";
               }elseif($vv['exp'] > 1000)
               {
                       $vv['level'] = "7";
               }
               $level[] = $vv;

        }
        foreach($level as $k=>$v)
        {
          DB::table('zy_userdetails')->where('uid',$v['uid'])->update(['level'=>$v['level']]);
        }
        return $level;

    }
    /**
     * 处理积分数据
     */
    static public function getScore()
    {
        $uid = session('login');
        $score = DB::table('zy_score')->where('uid',$uid)->first();
        return $score;
    }

    /**
     * 处理配置数据
     */
    static public function getConfig()
    {
        $config = DB::table('zy_config')->first();
        return $config;
    }
    /**
     * 创建一个构造函数,每次自动调用
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        //导航栏数据    分类 $cate_data
        $cate_data = self::getCatePid();
        //链接列表  $link_data
        $link = new Link;
        $link_data = $link->all();
        $login_user = self::getUser();
        $score = self::getScore();
        $level = self::getLevel();
        $config = self::getConfig();
        // dd($cate_data);

        view()->share('cate_data', $cate_data);
        view()->share('link_data', $link_data);
        view()->share('login_user', $login_user);
        view()->share('score', $score);
        view()->share('level', $level);
        view()->share('config', $config);
    }
}
